package com.mes.utils;

import java.sql.Connection;

import javax.sql.DataSource;

import cn.hutool.db.Db;
import cn.hutool.db.DbUtil;
import cn.hutool.db.ds.simple.SimpleDataSource;
import cn.hutool.db.handler.EntityListHandler;
import cn.hutool.db.sql.SqlExecutor;


public class SendMessageUtil {

	// 生产环境
		private static String DRIVER = "oracle.jdbc.driver.OracleDriver";
		private static String URL = "jdbc:oracle:thin:@192.168.33.27:1521/orcl";
		private static String USERNAME = "wms";
		private static String PASSWORD = "wms";
		private static DataSource ds2;
		private static EntityListHandler entityListHandler = new EntityListHandler();
		static {		
			ds2 = new SimpleDataSource(URL, USERNAME, PASSWORD, DRIVER);
			Db.use(ds2);
		}
	/**
	 * 通过消息编码，传入消息头、消息内容发送消息
	 * @param msgCode
	 * @param title
	 * @param text
	 */
	public static void sendMessage(String msgCode, String title, String text) {
		String sql = "{call wms.Mes_Message.send(?, ?, ?)}";
		Connection conn = null;
		try {
			conn = ds2.getConnection();
			SqlExecutor.execute(conn, sql,msgCode,title,text);
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (conn != null) {
				DbUtil.close(conn);
			}
		}
	}
	
	/**
	 * 通过消息编码，传入消息头、消息内容发送消息
	 * @param msgCode
	 * @param title
	 * @param text
	 */
	public static void sendMessage(String msgCode, String title, String text, String userId) {
		String sql = "{call wms.Mes_Message.send(?, ?, ?, '', ?)}";
		Connection conn = null;
		try {
			conn = ds2.getConnection();
//			conn.execute(sql, msgCode, title, text, userId);
			SqlExecutor.query(conn, sql, entityListHandler, msgCode, title, text, userId);
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (conn != null) {
				DbUtil.close(conn);
			}
		}
	}
	
	/**
	 * 通过消息编码发送消息提醒
	 * @param msgCode
	 */
	public static void sendMessage(String msgCode) {
		String sql = "{call wms.Mes_Message.send(?)}";
		Connection conn = null;
		try {
			conn = ds2.getConnection();
//			conn.execute(sql, msgCode);
			SqlExecutor.query(conn, sql, entityListHandler, msgCode);
			
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (conn != null) {
				DbUtil.close(conn);
			}
		}
	}
	/**
	 * 通过消息编码发送消息提醒
	 * @param msgCode
	 */
	public static void sendMessage(String msgCode, String userId) {
		String sql = "{call wms.Mes_Message.send(?, '', '', '', ?)}";
		Connection conn = null;
		try {
			conn = ds2.getConnection();
//			conn.execute(sql, msgCode, userId);
			SqlExecutor.query(conn, sql, entityListHandler, msgCode, userId);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (conn != null) {
				DbUtil.close(conn);
			}
		}
	}
	
}